// Copyright 2006 Mloofle Inc. All Rights Reserved.
import java.io.IOException;
import java.util.BitSet;

/**
 * Represents a line in a file in a corpus.
 */
public class CorpusLine {

  public static CorpusLine create(BitReader reader) throws IOException {
    CorpusLine result = null;
    while (reader.read()) {
      int nBits = Util.MIN_FILTER_SIZE;
      while (reader.read()) {
        nBits <<= 1;
      }

      // Read in the current filter
      BitSet bitSet = new MyBitSet(nBits);
      for (int i = 0; i < nBits; ++i) {
        if (reader.read()) {
          bitSet.set(i);
        }
      }

      if (result == null) {
        // We only care about the first filter in this implementation
        // Ignore subsequent filters
        result = new CorpusLine(bitSet);
      }
    }

    if (result == null) {
      return new CorpusLine();
    }
    return result;
  }

  public CorpusLine() {
    this.bitSet = null;
  }

  public CorpusLine(BitSet bitSet) {
    this.bitSet = bitSet;
  }

  public boolean matchesQuery(Query query) {
    if (bitSet == null) {
      return false;
    }
    return query.matchesFilter(bitSet);
  }

  private final BitSet bitSet;

}
